//
//  WoShouDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/13.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "WoShouDetailVC.h"
#import "WoFaYunDetailModel.h"
#import "WoShouDetailInfoCell.h"
#import "WoFaYunDetailGoodListCell.h"
#import "WoFaYunDetailOperationCell.h"
#import "WoFaYunDetailTimeLineCell.h"
#import "WoFaYunDetailGoodListVC.h"
#import "WoFaYunGuiJiVC.h"
#import "WoShouQianShouVC.h"

@interface WoShouDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) WoFaYunDetailModel *model;
@property (nonatomic, strong) UIView *bottomView;

@end

@implementation WoShouDetailVC

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

//查看运输轨迹
- (void)lookCarClick {
    WoFaYunGuiJiVC *vc = [WoFaYunGuiJiVC new];
    vc.wa_id = self.wa_id;
    vc.type = @"3";
    [self.rt_navigationController pushViewController:vc animated:YES];
}

//签收
- (void)qianShou {
    WoShouQianShouVC *vc = [WoShouQianShouVC new];
    vc.wa_id = self.wa_id;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(WoFaYunDetailModel *)model {
    if (!_model) {
        _model = [WoFaYunDetailModel new];
    }
    return _model;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryWoShouDetail params:@{@"wa_id":self.wa_id} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [WoFaYunDetailModel mj_objectWithKeyValues:responseObject];
            [self configUI];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void)configUI {
    if ([self.model.status_now intValue] < 8) {
        //签收之前状态
        [self.view addSubview:self.bottomView];
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *rejectBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [rejectBtn setBackgroundColor:HexColorA(0x3D68EB, 0.1)];
        rejectBtn.layer.masksToBounds = YES;
        rejectBtn.layer.cornerRadius = 6;
        [rejectBtn setImage:[UIImage imageNamed:@"wofayun_icon_location"] forState:0];
        [rejectBtn setTitle:@"查看运输轨迹" forState:0];
        [rejectBtn setTitleColor:Color_ThemeColor forState:0];
        rejectBtn.titleLabel.font = BoldFontOfSize(16);
        [rejectBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
        rejectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:rejectBtn];
        
        UIButton *passBtn = [[UIButton alloc] initWithFrame:CGRectMake(rejectBtn.nim_right + 15, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
        [passBtn setBackgroundColor:Color_ThemeColor];
        passBtn.layer.masksToBounds = YES;
        passBtn.layer.cornerRadius = 6;
        [passBtn setTitle:@"签收" forState:0];
        [passBtn setTitleColor:HexColor(0xffffff) forState:0];
        passBtn.titleLabel.font = BoldFontOfSize(16);
        [passBtn addTarget:self action:@selector(qianShou) forControlEvents:UIControlEventTouchUpInside];
        passBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        [self.bottomView addSubview:passBtn];
        
        self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
    }else {
        //签收及之后状态
        [self.view addSubview:self.bottomView];
        [self.bottomView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setImage:[UIImage imageNamed:@"icon_location"] forState:0];
        [nextBtn setTitle:@"查看运输轨迹" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
        self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([self.model.status_now intValue] < 8) {
        return 3;
    }else {
        return 4;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        WoShouDetailInfoCell *cell = [WoShouDetailInfoCell WoShouDetailInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell;
    }else if (indexPath.row == 1) {
        WoFaYunDetailGoodListCell *cell = [WoFaYunDetailGoodListCell WoFaYunDetailGoodListCellWithTableView:tableView];
        cell.model = self.model;
        __weak_self;
        cell.listBlock = ^(NSString * _Nonnull num) {
            WoFaYunDetailGoodListVC *vc = [WoFaYunDetailGoodListVC new];
            vc.wa_id = weakself.wa_id;
            vc.serial_num = num;
            vc.type = @"3";
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        return cell;
    }else {
        if ([self.model.status_now intValue] < 8) {
            if (indexPath.row == 2) {
                WoFaYunDetailTimeLineCell *cell = [WoFaYunDetailTimeLineCell WoFaYunDetailTimeLineCellWithTableView:tableView];
                cell.model = self.model;
                return cell;
            }
        }else {
            if (indexPath.row == 2) {
                WoFaYunDetailOperationCell *cell = [WoFaYunDetailOperationCell WoFaYunDetailOperationCellWithTableView:tableView];
                cell.model = self.model;
                return cell;
            }else {
                WoFaYunDetailTimeLineCell *cell = [WoFaYunDetailTimeLineCell WoFaYunDetailTimeLineCellWithTableView:tableView];
                cell.model = self.model;
                return cell;
            }
        }
    }
    UITableViewCell *cell = [UITableViewCell new];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        WoShouDetailInfoCell *cell = [WoShouDetailInfoCell WoShouDetailInfoCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else if (indexPath.row == 1) {
        WoFaYunDetailGoodListCell *cell = [WoFaYunDetailGoodListCell WoFaYunDetailGoodListCellWithTableView:tableView];
        cell.model = self.model;
        return cell.height;
    }else {
        if ([self.model.status_now intValue] < 8) {
            if (indexPath.row == 2) {
                WoFaYunDetailTimeLineCell *cell = [WoFaYunDetailTimeLineCell WoFaYunDetailTimeLineCellWithTableView:tableView];
                cell.model = self.model;
                return cell.height;
            }
        }else {
            if (indexPath.row == 2) {
                WoFaYunDetailOperationCell *cell = [WoFaYunDetailOperationCell WoFaYunDetailOperationCellWithTableView:tableView];
                cell.model = self.model;
                return cell.height;
            }else {
                WoFaYunDetailTimeLineCell *cell = [WoFaYunDetailTimeLineCell WoFaYunDetailTimeLineCellWithTableView:tableView];
                cell.model = self.model;
                return cell.height;
            }
        }
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
